<?php
/*
 * content : token service 服务类 
 * Author  : Songyicong 
 * time    : 2018-04-28 15:57:41 
 * email   : m13726262373@163.com 
 */
namespace app\api\service\v1;

use app\api\service\BaseService;
use app\api\model\v1\Admin as AdminModel;
use app\api\model\v1\AdminAuth as AdminAuthModel;
use think\Db;
use app\api\extra\Send;
use app\api\extra\Utils;

class TokenService extends BaseService{

    /** 更新 access_token */
    public function updateAccessToken($params = []){
        if(empty($params['refresh_token'])){
            return self::returnmsg(401, 'refresh_token is empty', [], []);
        }
        // 用 refresh_token 是否存在
        $results = AdminAuthModel::isReferToken($params['refresh_token']);
        if(!$results){
            return self::returnmsg(401, 'refresh_token is error', [], []);
        }
        if($results['refresh_time']<time()){
            return self::returnmsg(401, 'refresh_token expired', [], []);
        }
        // 开始更新token 
        $access_token = Utils::getToken($results['access_token'],$results['refresh_token']);
        $expires_in = time() + 7200;
        $data['access_token'] = $access_token;
        $data['expires_in'] = $expires_in;
        Db::name('admin_auth')->where('id',$results['id'])->update($data);
        // 生成令牌
        $accessTokenInfo = [
            'access_token' => $access_token, //访问令牌
            'expires_in' => $expires_in,
            'refresh_token' => $results['refresh_token'], //刷新令牌
            'refresh_time' => $results['refresh_time'], 
            'state'=>empty($params['state'])?'':$params['state']
        ];
        return self::returnmsg(200, 'success', $accessTokenInfo, []);
    }

    /** 设置 access_token */
    public function setAccessToken($params = []){
        if(empty($params['grant_type']) || 'client_credential' != $params['grant_type']){
            // 判断 grant_type 参数是否正确:client_credential
            return self::returnmsg(401, 'grant_type error', [], []);
        }
        if(empty($params['appid'])||empty($params['secret'])){
            return self::returnmsg(403, 'appid or secret error', [], []);
        }
        $results = AdminModel::getSecret($params['appid'],$params['secret']);
        
        if($results){
            $access_token = Utils::getToken($params['appid'],$params['secret']);
            $refresh_token = Utils::getToken($params['appid'],$access_token);
            $expires_in = time() + 7200;
            $refresh_time = time() + 3600*24;  
            // 保存数据到 km_admin_auth 表
            $data['access_token'] = $access_token;
            // 2小时内
            $data['expires_in'] = $expires_in;
            // 1天内
            $data['refresh_time'] = $refresh_time;
            $data['refresh_token'] = $refresh_token;
            $news = AdminAuthModel::isAdminId($results['id']);
            if(!$news){
                $data['create_time'] = time();
                $data['token_type'] = "client_credential";
                $data['admin_id'] = $results['id'];
                $data['auth_num'] = $results['auth_num'];
                Db::name('admin_auth')->insert($data);
            }else{
                Db::name('admin_auth')->where('id',$news['id'])->update($data);
            }
            // 生成令牌
            $accessTokenInfo = [
                'access_token' => $access_token, //访问令牌
                'expires_in' => $expires_in,
                'refresh_token' => $refresh_token, //刷新令牌
                'refresh_time' => $refresh_time, 
                'state'=>empty($params['state'])?'':$params['state']
            ];
            return self::returnmsg(200, 'success', $accessTokenInfo, []);
        }
        return self::returnmsg(403, 'appid or secret error', [], []);
    }
}
